$(document).ready(function() {
    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FIPINQSHT  : "FIPINQSHT",
        T_FBPBISOPE	 : 'FBPBISOPE',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FPPPRDOTIN : "FPPPRDOTIN"


    };
    /**
     * All controls's jquery object/text
     * @type {Object}
     */
    var controlsQuery = {
        W                  : $(window)             ,
        opeSelect          : $("#opeInfoSel")       ,
        toolId             : $("#equiCodeD"),
        toolIdSel          : $("#eqptCodeSel"),
        toolExplain        : $("#equiDesc"),
        toolOperateMode    : $("#equiModel"),
        toolStat      	   : $("#equiState"),
        toolType           : $("#equiSpec"),
        toolClass          : $("#formuCode")


    };

    /**
     * All button's jquery object
     * @type {Object}
     */
    var btnQuery = {
        query  : $("#query_btn"),
        moveout : $("#manually_remove_btn"),
        movein  : $("#manually_into_btn")
    };

    /**
     * All tool functions
     * @type {Object}
     */

    var toolFunc = {
        setOpeSelectDate : function(dataCnt, arr, selVal, selVal2, selTxt, selVal3, queryObj){
            var i, realCnt;
            queryObj.empty();
            queryObj.append("<option value= ''></option>");
            realCnt = parseInt( dataCnt, 10);
            queryObj.append()
            if( realCnt == 1 ){
                queryObj.append("<option value="+ arr[selVal] + "@" + arr[selVal2] +"@"+arr[i][selVal3]+">"+ arr[selTxt] +"</option>");
            }else if( realCnt > 1 ){
                for( i = 0; i < realCnt; i++ ){
                    queryObj.append("<option value="+ arr[i][selVal] + "@" + arr[i][selVal2] + "@"+arr[i][selVal3]+">"+ arr[i][selTxt] +"</option>");
                }
            }
            queryObj.select2({
                theme : "bootstrap"
            });
        },
        iniOpeSelect : function(){
            SelectDom.setSelect($("select"), "", "");

            var inObj, outObj;
            var ope_arr = new Array();
            inObj = {
                trx_id      : VAL.T_FBPBISOPE,
                action_flg  : 'L'
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                for(var i=0;i<outObj.tbl_cnt;i++){
                    if(outObj.oary[i].tool_run_mode != "UPK"){
                        ope_arr.push(outObj.oary[i]);
                    }
                }
                toolFunc.setOpeSelectDate(ope_arr.length, ope_arr, "ope_id", "ope_ver", "ope_dsc","tool_run_mode", controlsQuery.opeSelect);
            }
        },
        iniToolSel: function(toolObj,opeObj){
            toolObj.empty();
            var opeSelVal = opeObj.find("option:selected").val();
            if(!opeSelVal){
                return false;
            }
            if(opeSelVal){
                var selectedMode = opeSelVal.split("@")[2];
            }
            var inObj, outObj;
            var item = "tool_id";
            var iary = {
                tool_run_mode : selectedMode,
                unit_typ      : "MAIN"
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		 : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(toolObj, outObj.oary, "tool_id","tool_id","",true);
            }
            toolObj.select2({
                theme : "bootstrap"
            });
        },
        iniToolInfo:function(toolObj,explainObj,operateObj,statObj,typeObj,classObj,statExplainObj){
            var tool_id,tool_dsc,tool_ctrl_mode,tool_stat,unit_typ,tool_cate,toolStat_dsc;
            var tool_id = toolObj.find("option:selected").val();
            if(!tool_id){
                return false;
            }
            var iary = {
                tool_id : tool_id
            };
            var inTrxObj = {
                trx_id     : VAL.T_FBPBISTOL,
                action_flg : 'I'        ,
                iary       : [iary]
            }
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                tool_id = outTrxObj.oary[0]["tool_id"];
                tool_dsc =outTrxObj.oary[0]["tool_dsc"];
                tool_ctrl_mode =outTrxObj.oary[0]["tool_ctrl_mode"];
                tool_stat =outTrxObj.oary[0]["tool_stat"];
                unit_typ =outTrxObj.oary[0]["unit_typ"];
                tool_cate =outTrxObj.oary[0]["tool_cate"];

                var iaryA = {
                    data_cate : 'EQST'   ,
                    data_id	  : tool_stat
                };
                var inObj = {
                    trx_id      : 'FBPBISDAT' ,
                    action_flg : 'Q'        ,
                    iary        : [iaryA]
                };
                var  outObj = comTrxSubSendPostJson(inObj);

                $("#equiCodeD").html(tool_id);
                explainObj.html(tool_dsc);
                operateObj.html(tool_ctrl_mode);
                statObj.html(tool_stat);
                typeObj.html(unit_typ);
                classObj.html(tool_cate);

            };
        },
        strVer : function () {
            var id = $("#opeInfoSel").select2("data")[0].id;
            var index = id.indexOf("@");
            var end = id.lastIndexOf("@")
            var path = id.substr(index+1,end-index-1);
            return path;
        }
    };

    /**
     * All button click function
     * @type {Object}
     */
    var btnFunc = {
        //查询玻璃信息根据玻璃代码
        query_func : function () {
            var sheetCode = $("#sheetCode").val();
            var inObj, outObj;
            inObj = {
                prd_seq_id: sheetCode,
                trx_id: VAL.T_FIPINQSHT,
                action_flg: 'Q',
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                $("#sheetCodeD").text(outObj.prd_seq_id);
                $("#sheetState").text(outObj.prd_stat);
                $("#glassCount").text(outObj.prd_qty);
                $("#path_id").text(outObj.cr_path_id_fk);
                $("#path_ver").text(outObj.cr_path_ver_fk);
                $("#siteInfoD").text(outObj.cr_ope_no_fk);
                $("#curPosition").text(outObj.cr_ope_id_fk);
                $("#workOrdNum").text(outObj.wo_id_fk);
                $("#tool_id").text(outObj.cr_tool_id_fk);
            };

        },
        //手动取出
        manually_remove : function () {


            inObj = {
                trx_id      : VAL.T_FPPPRDOTIN,
                action_flg  : 'R',
                sheet_id    : $("#sheetCode").val(),
                cr_path_ver : toolFunc.strVer(),
                ope_info    : $("#opeInfoSel").val(),
                tool_id     : $("#eqptCodeSel").val(),
                ip_address  : $("#address").val(),
                evt_user     : VAL.EVT_USER
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("取出成功！");
            };
        },

        //手动放入
        manually_movein : function () {

            inObj = {
                trx_id      : VAL.T_FPPPRDOTIN,
                action_flg  : 'I',
                sheet_id    : $("#sheetCode").val(),
                cr_path_ver : toolFunc.strVer(),
                ope_info    : $("#opeInfoSel").val(),
                tool_id     : $("#eqptCodeSel").val(),
                ip_address  : $("#address").val()
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("放入成功！");
            };
        }

    };
    var iniContorlData = function(){
        //Ope info
        toolFunc.iniOpeSelect();
        //Tool info
        toolFunc.iniToolSel(controlsQuery.toolIdSel,controlsQuery.opeSelect);
        controlsQuery.opeSelect.change(function(){
            toolFunc.iniToolSel(controlsQuery.toolIdSel,controlsQuery.opeSelect);
            controlsQuery.toolId.text("");
            controlsQuery.toolExplain.text("");
            controlsQuery.toolOperateMode.text("");
            controlsQuery.toolStat.text("");
            controlsQuery.toolType.text("");
            controlsQuery.toolClass.text("");
        });
        controlsQuery.toolIdSel.change(function(){
            toolFunc.iniToolInfo(controlsQuery.toolIdSel,controlsQuery.toolExplain,
                controlsQuery.toolOperateMode,controlsQuery.toolStat,controlsQuery.toolType,
                controlsQuery.toolClass,controlsQuery.toolStatExplain);
        });

    };


    if($.trim($("#sheetCode").val())!=null){
        $("#sheetCode").keydown(function(event){
            event=document.all?window.event:event;
            if((event.keyCode || event.which)==13){
                btnFunc.query_func();
                return false;
            }
        });
    }
    //模态框拖动
    $(".modal-content").draggable();

    /**
     * Bind button click action
     */

    btnQuery.query.click(function(){
        btnFunc.query_func();
    });
    btnQuery.moveout.click(function () {
        btnFunc.manually_remove();
    });
    btnQuery.movein.click(function () {
        btnFunc.manually_movein();
    });
    var initializationFunc = function(){
        $("#sheetCode").val("");
        iniContorlData();
    };
    initializationFunc();

});

